package net.hpxn.gsmc.core.www.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.hpxn.gsmc.core.server.Server;
import net.hpxn.gsmc.core.server.ServerManager;

public class StatusServlet extends HttpServlet {
	private static final long serialVersionUID = 80973134825163449L;
	
	private List<Server> servers;
	
	public StatusServlet() {
		this.servers = ServerManager.getServerList();
	}

	@Override
	protected void doGet( HttpServletRequest req, HttpServletResponse resp )
			throws ServletException, IOException {
		resp.addHeader( "Content-Type", "text/xml" );
		resp.addHeader( "Access-Control-Allow-Origin", "*" ); // Hmm...
		resp.addHeader( "Pragma", "no-cache" );
		resp.addHeader( "Cache-Control", "no-cache" );
		resp.addDateHeader( "Expires", 0 );
		
		resp.getWriter().write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
		
		String wServerName = req.getParameter( "name" );
		if ( wServerName != null && !wServerName.equals( "" ) ) {
			resp.getWriter().write( "  <server>\n" );
			for ( Server wServer : servers ) {
				if ( wServer.getName().equalsIgnoreCase( wServerName ) ) {
					
					resp.getWriter().write( "    <name>" + wServer.getName() + "</name>\n" );
					resp.getWriter().write( "    <available>" + wServer.isUp() + "</available>\n" );
					resp.getWriter().write( "    <numPlayers>" + wServer.getPlayerCount() + "</numPlayers>\n" );
					resp.getWriter().write( "    <maxPlayers>" + wServer.getMaxPlayers() + "</maxPlayers>\n" );
					resp.getWriter().write( "    <hostname>" + wServer.getHostname() + "</hostname>\n" );
					resp.getWriter().write( "    <address>" + wServer.getRemoteIp() + "</address>\n" );
					resp.getWriter().write( "    <port>" + wServer.getRemotePort() + "</port>\n" );
					resp.getWriter().write( "    <version>" + wServer.getVersion() + "</version>\n" );
					resp.getWriter().write( "    <map>" + wServer.getMapName() + "</map>\n" );
					resp.getWriter().write( "    <game>" + wServer.getGameType() + "</game>\n" );
					resp.getWriter().write( "    <group>" + wServer.getGroup() + "</group>\n" );
					resp.getWriter().write( "  </server>\n" );
				}
			}
			resp.getWriter().write( "  </server>\n" );
		} else {
			resp.getWriter().write( "<servers>\n" );
			for ( Server wServer : servers ) {
				resp.getWriter().write( "  <server>\n" );
				resp.getWriter().write( "    <name>" + wServer.getName() + "</name>\n" );
				resp.getWriter().write( "    <available>" + wServer.isUp() + "</available>\n" );
				resp.getWriter().write( "    <numPlayers>" + wServer.getPlayerCount() + "</numPlayers>\n" );
				resp.getWriter().write( "    <maxPlayers>" + wServer.getMaxPlayers() + "</maxPlayers>\n" );
				resp.getWriter().write( "    <hostname>" + wServer.getHostname() + "</hostname>\n" );
				resp.getWriter().write( "    <address>" + wServer.getRemoteIp() + "</address>\n" );
				resp.getWriter().write( "    <port>" + wServer.getRemotePort() + "</port>\n" );
				resp.getWriter().write( "    <version>" + wServer.getVersion() + "</version>\n" );
				resp.getWriter().write( "    <map>" + wServer.getMapName() + "</map>\n" );
				resp.getWriter().write( "    <game>" + wServer.getGameType() + "</game>\n" );
				resp.getWriter().write( "    <group>" + wServer.getGroup() + "</group>\n" );
				resp.getWriter().write( "  </server>\n" );
			}
			resp.getWriter().write( "</servers>\n" );
		}
	}
	
}
